<?php

namespace app\expertReport\controller;

use app\common\controller\PCBase;
// use app\common\controller\DBase;//测试专用
use app\expertReport\logic\ExpertShowcaseLogic;
use app\expertReport\model\ExpertShowcase as ExpertShowcaseModel;
use think\facade\Db;
use Throwable;
use app\common\library\Upload;

class ExpertShowcase extends PCBase
{
    /**
     * 模型
     * @var object
     * @phpstan-var ExpertShowcaseModel
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new ExpertShowcaseModel();
    }

    /**
     * index
     * @throws Throwable
     */
    public function index()
    {
        $params     = $this->request->post();
        $ExpertShowcaseLogic = new ExpertShowcaseLogic();
        return $ExpertShowcaseLogic->index($params);
    }


    /**
     * 添加
     * @throws Throwable
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $ExpertShowcaseLogic = new ExpertShowcaseLogic();
            $result = $ExpertShowcaseLogic->add($data);
            return $result;
        }
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit($id = null)
    {
        $row = $this->model->find($id);
        if (!$row) {
            return json(DefaultResponse(201, false, '记录不能为空!', null));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($this->modelValidate) {
                try {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = new $validate();
                    $validate->scene('edit')->check($data);
                } catch (Throwable $e) {
                    return json(DefaultResponse(201, false, $e->getMessage(), null));
                }
            }

            $ExpertShowcaseLogic = new ExpertShowcaseLogic();
            $result = $ExpertShowcaseLogic->edit($row, $data);

            return $result;
        }
        //回显
        if(!empty($row['createtime'])){
            $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
        }
        if(!empty($row['sales'])){
            $row['sales'] = (float)$row['sales'];
        }
        if(!empty($row['commission_expenses'])){
            $row['commission_expenses'] = (float)$row['commission_expenses'];
        }
        if(!empty($row['ordernum'])){
            $row['ordernum'] = (int)$row['ordernum'];
        }
        return json(DefaultResponse(200, true, '获取成功', ['row' => $row]));
    }

    /**
     * 删除
     * @param null $ids
     * @throws Throwable
     */
    public function del($ids = null)
    {

        $data = $this->request->post();
        if (!$data||!$ids){
            return  json(DefaultResponse(201, false, '参数错误', null));
        }

        $pk    = $this->model->getPk();
        $data  = $this->model->where($pk, 'in', $ids)->select();
        
        $count = 0;
        $this->model->startTrans();
        try {
            foreach ($data as $v) {
                if ($v->id != $this->getAdminId()) {
                    $count += $v->delete();
                    $this->model
                        ->where('id', $v['id'])
                        ->delete();
                }
            }
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return   json(DefaultResponse(201, false, $e->getMessage(), null));
        }
        if ($count) {
            return   json(DefaultResponse(200, true, '操作成功', null));
        } else {
            return   json(DefaultResponse(201, false, '操作失败', null));
        }
    }
}
